//
//  ViewController.swift
//  正则表达式
//
//  Created by zhangguo on 15/10/25.
//  Copyright © 2015年 zhangguo. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
       let string = "<a href=\"http://weibo.com/\" rel=\"nofollow\">微博 weibo.com</a>"
        
        let result = string.hrefLink()
        print(result.link)
        print(result.text)
    }
    
    
    func demo() {
        
        let string = "<a href=\"http://weibo.com/\" rel=\"nofollow\">微博 weibo.com</a>"
        //1找特征字符
        let pattern = "<a href=\"(.*?)\".*?>(.*?)</a>"
        
        let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.DotMatchesLineSeparators)
        
        //开始匹配
        let result = regex.firstMatchInString(string, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, string.characters.count))
        
        print(result?.numberOfRanges)
        
        
        let r = result!.rangeAtIndex(1)
        let r2 = result!.rangeAtIndex(2)
        
        print((string as NSString).substringWithRange(r))
        print((string as NSString).substringWithRange(r2))
    }
}

